﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using FlyCode.DBUtility;
namespace com.DAL.laibendi
{
    /// <summary>
    /// 数据访问类:T_Member
    /// </summary>
    public class T_Member
    {
        public T_Member()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsUserName(String UserName,String Passport)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Member");
            strSql.Append(" where UserName=@UserName and Passport=@Passport ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar ),new SqlParameter("@Passport", SqlDbType.NVarChar )};
            parameters[0].Value = UserName;
            parameters[1].Value = Passport;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(com.Model.laibendi.T_Member model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Member(");
            strSql.Append("UserName,Passport,Email,Mobile,Province,City,Country,Address,RegisterIp,RegisterTime)");
            strSql.Append(" values (");
            strSql.Append("@UserName,@Passport,@Email,@Mobile,@Province,@City,@Country,@Address,@RegisterIp,@RegisterTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Passport", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.NVarChar,20),
					new SqlParameter("@City", SqlDbType.NVarChar,20),
					new SqlParameter("@Country", SqlDbType.NVarChar,20),
					new SqlParameter("@Address", SqlDbType.NVarChar,50),
					new SqlParameter("@RegisterIp", SqlDbType.NVarChar,50),
					new SqlParameter("@RegisterTime", SqlDbType.DateTime)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.Passport;
            parameters[2].Value = model.Email;
            parameters[3].Value = model.Mobile;
            parameters[4].Value = model.Province;
            parameters[5].Value = model.City;
            parameters[6].Value = model.Country;
            parameters[7].Value = model.Address;
            parameters[8].Value = model.RegisterIp;
            parameters[9].Value = model.RegisterTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(com.Model.laibendi.T_Member model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Member set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Passport=@Passport,");
            strSql.Append("Email=@Email,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("Province=@Province,");
            strSql.Append("City=@City,");
            strSql.Append("Country=@Country,");
            strSql.Append("Address=@Address,");
            strSql.Append("RegisterIp=@RegisterIp,");
            strSql.Append("RegisterTime=@RegisterTime");
            strSql.Append(" where MemberId=@MemberId");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberId", SqlDbType.BigInt,8),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@Passport", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.NVarChar,20),
					new SqlParameter("@City", SqlDbType.NVarChar,20),
					new SqlParameter("@Country", SqlDbType.NVarChar,20),
					new SqlParameter("@Address", SqlDbType.NVarChar,50),
					new SqlParameter("@RegisterIp", SqlDbType.NVarChar,50),
					new SqlParameter("@RegisterTime", SqlDbType.DateTime)};
            parameters[0].Value = model.MemberId;
            parameters[1].Value = model.UserName;
            parameters[2].Value = model.Passport;
            parameters[3].Value = model.Email;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.Province;
            parameters[6].Value = model.City;
            parameters[7].Value = model.Country;
            parameters[8].Value = model.Address;
            parameters[9].Value = model.RegisterIp;
            parameters[10].Value = model.RegisterTime;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long MemberId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Member ");
            strSql.Append(" where MemberId=@MemberId");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberId", SqlDbType.BigInt)
};
            parameters[0].Value = MemberId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string MemberIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Member ");
            strSql.Append(" where MemberId in (" + MemberIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public com.Model.laibendi.T_Member GetModel(long MemberId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 MemberId,UserName,Passport,Email,Mobile,Province,City,Country,Address,RegisterIp,RegisterTime from T_Member ");
            strSql.Append(" where MemberId=@MemberId");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberId", SqlDbType.BigInt)

                                        };
            parameters[0].Value = MemberId;

            com.Model.laibendi.T_Member model = new com.Model.laibendi.T_Member();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["MemberId"].ToString() != "")
                {
                    model.MemberId = long.Parse(ds.Tables[0].Rows[0]["MemberId"].ToString());
                }
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                model.Passport = ds.Tables[0].Rows[0]["Passport"].ToString();
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                model.Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                model.Province = ds.Tables[0].Rows[0]["Province"].ToString();
                model.City = ds.Tables[0].Rows[0]["City"].ToString();
                model.Country = ds.Tables[0].Rows[0]["Country"].ToString();
                model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                model.RegisterIp = ds.Tables[0].Rows[0]["RegisterIp"].ToString();
                if (ds.Tables[0].Rows[0]["RegisterTime"].ToString() != "")
                {
                    model.RegisterTime = DateTime.Parse(ds.Tables[0].Rows[0]["RegisterTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select MemberId,UserName,Passport,Email,Mobile,Province,City,Country,Address,RegisterIp,RegisterTime ");
            strSql.Append(" FROM T_Member ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" MemberId,UserName,Passport,Email,Mobile,Province,City,Country,Address,RegisterIp,RegisterTime ");
            strSql.Append(" FROM T_Member ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_Member";
            parameters[1].Value = "MemberId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

